Clean Code 笔记(1)Name

Reveal Your Intent

use intent-revealing name, if your need a comment ,that's not a good name.

  • bad:

    1
    int d; // elapsed time in days

  • good

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    int elapsedTimeInDays;
    ```
    # Descpribe Your Problem
    * bad:
    ```java
    /***Useful range constant*/
    public static final int INLCUDE_NONE=0;
    /***Useful range constant*/
    public static final int INLCUDE_NONE=1;
    /***Useful range constant*/
    public static final int INLCUDE_NONE=2;
    /***Useful range constant*/
    public static final int INLCUDE_NONE=3;

can you tell me what they mean,did the comment helpful? 'userful?' are their useless code?'range?'range of what?'Constant' ofco use its a constant

whenever you have to read the code in order to understand the name, the name has pretty much failed to communicate the intent.It's a bad name. Remember names are not for your convienient, it's your primariy tools for communicate intent.Commuicate your intent is always your first priority,it's even more important than the code work.

Avoid Disinformation

  • disinformation: name in code does not mean what it's said.(worsest sins) a misleading name can cous

Pronounceable Names

  • bad
    1
    2
    3
    4
    5
    6
    7
    8
    9
    int PC_GWDA;
    public int getYYYY(){
    return this.year;
    }
    int qty_tests =0;
    int qty_pass_m=0;
    int qty_pass_s =0;
    int qty_skip = 0;
    int qty_fails=0;

Avoid Encoding

prifix: p mean point,c mean char, b mean boolean.C for Class,I for Interface. C: Class CAccount I:Interface IAccount p: pointer pAccount s: String sName st: Null Terminated StName

now days,IDE can tell all this informations,i won't be using them.That' 1990s legency,silly prifixs.Just use names! Let the IDE,compiller and test do the rest. 'Account is better than IAccount'

Part of Speech

class and variable are norns or norn pharse,methods are verbs. ignore silly nosie words.

  • good ACOUUNT,MESSAGEPARSER

  • silly nosie words MANAGER,DATA,INFO,PROSSOR

  • boolean variabels should been write like predicats,they read well,like isEmpty,isTerminated

  • methods should been verbs or verbs pharese, they read well,like getPirce,postPayment

  • if a methods returns a boolean,then should be predicats,beacuse they likely be used in if statement and they read well,likeisPostable

    1
    2
    3
    4
    5
    6
    7
    8
    boolean isEmpty;
    boolean isTerminated;
    ---
    if(isEmpty){
    if (payment.isPostable()){
    postPayment(payment);
    }
    }

  • Enums tends to be states or object descriptors,so they often adj.

    1
    2
    3
    enum Color{RED,GREEN,BLUE};
    enum Status{PENDING,CLOSED,CANCELLED};
    enum Size{SMALL,MEDIUM,LARGE};

remmeber: it's a lot easier to read code if the statements form sentences that read like well written poems.

1
if(employee.isLate() employee.reprimand();

The Scope of Length Rule

  • for varibales:

the longger the scope of variable,the longger veriable names,but the variable with short scope should have short names;

1
2
3
4
for (ITestResult tr: m_configIssues){
Elemtent element = createElement(d,tr);
rootElement.appendChild(element);
}

here tr is a good name,but d is bad.element should be shorten to e.

  • for funtions(oppsite):

the longer the scope,the shorter the function or name should be; the shortter the scope,the longer the function should be.

1
2
3
4
5
6
7
8
9
10
public void server(Socket s){
try{
tryProcessInstructions(s);
}catch(Throwable e){
slimFactory.stop();
close();
closeEnclosingServiceInSeperateThread();
}
}
}

we like long socpe public function names short like serve,convinient to use,long name hard to tell apart;we like short scope private function names long like tryProcessInstructions,closeEnclosingServiceInSeperateThread,only called from here ,it's kind of a comment, explain themselves. The same arguement can be applied to Classes.

请我喝杯咖啡吧!